<?php
include_once("convert.php");
include_once("../info_struct.php");
include_once("connection.php");

/*
 * Usage:
 * addRef(ref);
 * Parameter:
 * ref - infos object containing the reference that is to be added
 * to the database
 * Return:
 * The id of the added/updated reference in case of success and false
 * in case of failure
 */
function add_ref($ref) {
    /* Connect with database */
    $con = ldb_connect();
    
    
    /* Check if given infos object contains an id
     * if that is the case it is already contained in the database and
     * only needs to be updated
     */
    if ($ref->id) {
        /* Generate SQL-query for updating the reference */
        $sql = infos_2_sql_update($ref);
        /* Execute query on the database */
        $res = ldb_query($con, $sql);
        /* If update was successful and the infos object contains
         * a not empty note update the note, too
         */
        if ($res && $ref->note && strlen($ref->note) > 0) {
            if ($ref->note_id && strlen($ref->note_id) > 0) {
                /* Generate SQL-query for updating the note */
                $sqln = note_2_sql_update($ref);
            } else {
                /* Generate SQL-query for inserting the note */
                $sqln = note_2_sql_insert($ref);
            }
            
            /* Execute query on the databse */
            $res = ldb_query($con, $sqln);
        }
        /* Disconnect with the databse */
        ldb_disconnect($con);
        if (!$res) {
            return false;
        } else {
            return $ref->id;
        }
        
    } else {
        /* Check if references with same characteristics is
         * already contained in the database
         */
        $check = check_ref($con, $ref);
        if ($check) {
            return $check->id;
        }
        
        /* Generate SQL for inserting a reference into the database */
        $sql = infos_2_sql_insert($ref);
        /* Query the database */
        if (ldb_query($con, $sql)) {
            /* Get id of the reference in the databse */
            $ref->id = mysqli_insert_id($con);
            /* If the infos object contains a not empty note
             * add it to the ref_note-table.
             */
            if ($ref->note && strlen($ref->note) > 0) {
                /* Generate SQL-query for adding the note to the ref_note
                 * table
                 */
                $sql_note = note_2_sql_insert($ref);
                /* Execute query on the database */
                if (ldb_query($con, $sql_note)) {
                    /* Disconnect and return the id of the reference */
                    ldb_disconnect($con);
                    return $ref->id;
                } else {
                    ldb_disconnect($con);
                    return false;
                }
            }
            ldb_disconnect($con);
            return $ref->id;
        } else {
            ldb_disconnect($con);
            return false;
        }
    }
}

/*
 * Usage:
 * check_ref(con, ref);
 * Parameter:
 * con - Connection with database
 * ref - infos object containing a reference
 * Return:
 * False - If a reference with the information of ref does not exist
 *     in the database
 * infos - The first hit containing the information of ref
 */
function check_ref($con, $ref) {
    if ($ref->doi) {
        $sql = infos_2_sql_search_doi($ref);
    } else if ($ref->zmath) {
        $sql = infos_2_sql_search_zmath($ref);
    } else if ($ref->arxiv) {
        $sql = infos_2_sql_search_arxiv($ref);
    } else {
        $sql = infos_2_sql_search_exact($ref);
    }
    
    $res = ldb_query($con, $sql);
    if (!$res) {
        return false;
    }
    $ret = sql_result_2_infos($res);
    
    if (count($ret) == 0) {
        return false;
    }
    
    return $ret[0];
}

/*
 * Usage:
 * add_ref_xml(ref);
 * Parameter:
 * ref - infos object containing reference that is extracted from
 *     the xml-reference-file.
 * Return:
 * True in case of success and false if not
 * 
 * IMPORTANT: Only to be used in combination with the old xml-files!
 */
function add_ref_xml($ref) {
    /* Connect with local database */
    $con = ldb_connect();
    /* Generate SQL-query for inserting the reference */
    $sql = infos_2_sql_insert_xml($ref);
    /* Execute query on the database */
    $res = ldb_query($con, $sql);
    if ($res) {
        /* Generate SQL-query for inserting the note */
        $sqln = note_2_sql_insert($ref);
        /* Execute query on the database */
        $res = ldb_query($con, $sqln);
    }
    /* Disconnect database */
    ldb_disconnect($con);
    if (!$res) {
        return false;
    } else {
        return true;
    }
}

/*
 * Usage:
 * updateRef($ref);
 * Parameter:
 * ref - infos object containing the reference that is to be updated
 * must contain an id!
 * Return:
 * true in case of success and false in case of failure
 * 
 * IMPORTANT: Currently not used in the system.
 */
function update_ref($ref) {
    // Generate SQL for updatinge a reference in the database
    $sql = infos_2sql_update($ref);
    //Query the database
    if (ldb_query_complete($sql)) {
        return true;
    } else {
        return false;
    }
}

